<?php

namespace app\admin\controller\sponsor;

use app\common\controller\Backend;
use Exception;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use app\admin\model\trades\assets\Card_log;

/**
 * 举办人设置类
 *
 * @icon fa fa-circle-o
 */
class Sponsororder extends Backend
{


    protected $model = null;
    /**
     * 快速搜索时执行查找的字段
     */
    //protected $searchFields = 'planName';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('SponsorOrder');
        //$this->model = new \app\admin\model\trades\assets\Card;
        //print_r($this->model->getCardList());die;
        //$this->view->assign('cardList',$this->model->getCardList());
    }

    public function import()
    {
        parent::import();
    }


    /**
     * 设置入口
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post("type");
            $status = $this->request->post("status");
            $status =($status==1)?2:1;

            $result = Db::name("sponsor_panel")->where('id',$id)->update(array('status'=>$status));
        }
        $pData = Db::name("sponsor_panel")->select();
        $this->view->assign('pData', empty($pData)?array():$pData);
        return $this->view->fetch();
    }
    /**
     * 订单详情
     */
    public function orderdetail()
    {
        return $this->view->fetch();

    }
    /**
     * 举办人订单
     */
    public function list()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams('planName');
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }

        return $this->view->fetch();
    }
    /**
     * 查看和编辑，举办人设置类型
     */
    public function sponsororderedit()
    {
        $type = $this->request->request('type');
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params['update_time'] = time();
                $params['type'] = $type;
                $params['iLeiji'] = empty($params['iLeiji'])?0:1;
                $params['iZuigao'] = empty($params['iZuigao'])?0:1;
                $params['iFangjian'] = empty($params['iFangjian'])?0:1;
                $params['iDange'] = empty($params['iDange'])?0:1;

                if($type ==6 || $type ==7 ){
                    $params['iMonth'] = empty($params['iMonth'])?0:1;
                    $params['iQuarter'] = empty($params['iQuarter'])?0:1;
                    $params['iYear'] = empty($params['iYear'])?0:1;
                    $params['i2year'] = empty($params['i2year'])?0:1;
                    $params['i3year'] = empty($params['i3year'])?0:1;
                    $params['i4year'] = empty($params['i4year'])?0:1;
                    $params['iExpand'] = empty($params['iExpand'])?0:1;
                }
                if( in_array($type, array('1','2','3','4'))) {
                    $params['iFreeHours'] = empty($params['iFreeHours']) ? 0 : 1;
                    $params['iYearFee'] = empty($params['iYearFee']) ? 0 : 1;
                    $params['iQuarterFee'] = empty($params['iQuarterFee']) ? 0 : 1;
                    $params['iMonthFee'] = empty($params['iMonthFee']) ? 0 : 1;
                    $params['iLiuliangPrice'] = empty($params['iLiuliangPrice']) ? 0 : 1;
                }

                $result = Db::name("sponsor_order_set")->where('type',$type)->find();
                if(empty($result)){
                    $result = Db::name("sponsor_order_set")->insert($params);

                    if(isset($params['iExpand'])&& !empty($params['iExpand'])){
                        //插入虚拟活动室
                        $this->setVirtualActivityRoom($type);
                    }

                }else{
                    $result = Db::name("sponsor_order_set")->where('type',$type)->update($params);
                    if(isset($params['iExpand'])&& !empty($params['iExpand'])){
                        //插入虚拟活动室
                        $this->setVirtualActivityRoom($type);
                    }
                }
            }
        }
        $setting = Db::name("sponsor_order_set")->where('type', $type)->find();

        $this->view->assign('r', empty($setting)?array():$setting);
        if($type==6 || $type==7) {
            $vRoomsData = Db::name("v_activity_room_set")->where('type', $type)->select();
            $this->view->assign('vRoomsData', empty($vRoomsData) ? array() : $vRoomsData);
        }

        switch ($type) {
            case '1':
                $this->view->assign("deition",'活动个人版');
                $this->view->assign("type",'1');
                return $this->view->fetch('sponsor/sponsororder/sponsororderPersonalBasic');
                break;
            case '2':
                $this->view->assign("deition",'活动高级版');
                $this->view->assign("type",'2');
                return $this->view->fetch('sponsor/sponsororder/sponsororderPersonalPlus');
                break;
            case '3':
                $this->view->assign("deition",'活动商业版');
                $this->view->assign("type",'3');
                return $this->view->fetch('sponsor/sponsororder/sponsororderPersonalBusiness');
                break;
            case '4':
                $this->view->assign("deition",'活动企业版');
                $this->view->assign("type",'4');
                return $this->view->fetch('sponsor/sponsororder/sponsororderPersonalEnterprise');
                break;
            case '5':
                $this->view->assign("deition",'会议个人版');
                $this->view->assign("type",'5');
                return $this->view->fetch('sponsor/sponsororder/sponsororderFree');
                break;
            case '6':
                $this->view->assign("deition",'会议商业版');
                $this->view->assign("type",'6');
                return $this->view->fetch('sponsor/sponsororder/sponsororderBusiness');
                break;
            case '7':
                $this->view->assign("deition",'会议企业版');
                $this->view->assign("type",'7');
                return $this->view->fetch('sponsor/sponsororder/sponsororderEnterprise');
                break;
        }

    }
    /*
     * 多个虚拟活动室设置
     * pram $typeId string 类型Id
     */
    private function  setVirtualActivityRoom($typeId){
        if (empty($typeId)) return false;
        $pData = $this->request->post();
        if($pData['vName'] && count($pData['vName']) >0){
            Db::name("v_activity_room_set")->where('type',$typeId)->delete(true);
            for($i=0;$i<count($pData['vName']);$i++){
                $nData['type'] = $typeId;
                $nData['vName'] = $pData['vName'][$i];
                $nData['vDesc'] = $pData['vDesc'][$i];
                $nData['vTotal'] = $pData['vTotal'][$i];
                $nData['vPrice'] = $pData['vPrice'][$i];
                $nData['vBuyMinNum'] = $pData['vBuyMinNum'][$i];
                $result = Db::name("v_activity_room_set")->insert($nData);
                if($result) unset($nData);
            }
        }
        return true;
    }



}
